class Solution {//复制粘贴的官方，理解了，还不会写
    Map<Character, Integer> ori = new HashMap<Character, Integer>();
    Map<Character, Integer> cnt = new HashMap<Character, Integer>();

    public String minWindow(String s, String t) {
        int tLen = t.length();
        for (int i = 0; i < tLen; i++) {
            char c = t.charAt(i);
            ori.put(c, ori.getOrDefault(c, 0) + 1);
        }
        int l = 0, r = -1;
        int len = Integer.MAX_VALUE, ansL = -1, ansR = -1;
        int sLen = s.length();
        while (r < sLen) {
            ++r;
            if (r < sLen && ori.containsKey(s.charAt(r))) {
                cnt.put(s.charAt(r), cnt.getOrDefault(s.charAt(r), 0) + 1);
            }
            while (check() && l <= r) {
                if (r - l + 1 < len) {
                    len = r - l + 1;
                    ansL = l;
                    ansR = l + len;
                }
                if (ori.containsKey(s.charAt(l))) {
                    cnt.put(s.charAt(l), cnt.getOrDefault(s.charAt(l), 0) - 1);
                }
                ++l;
            }
        }
        return ansL == -1 ? "" : s.substring(ansL, ansR);
    }

    public boolean check() {
        Iterator iter = ori.entrySet().iterator(); 
        while (iter.hasNext()) { 
            Map.Entry entry = (Map.Entry) iter.next(); 
            Character key = (Character) entry.getKey(); 
            Integer val = (Integer) entry.getValue(); 
            if (cnt.getOrDefault(key, 0) < val) {
                return false;
            }
        } 
        return true;
    }
}

class Solution {//自己写的，该代码只支持没有重复字符的字符串
    public String minWindow(String s, String t) {
        int sLength = s.length();
        int tLength = t.length();
        String num = "";
        String ans = "";
        if(sLength < tLength || t == "") {
            return ans;
        }
        Set<Character> set = new HashSet<Character>();
        for(int i = 0;i < tLength;i++) {
            set.add(t.charAt(i));
        }
        for(int i = 0;i < sLength;i++) {
            int rigth = i;
            if(!set.contains(s.charAt(i))) {
                continue;
            } else {
                while(rigth < sLength) {
                    if(set.contains(s.charAt(rigth))) {
                        set.remove(s.charAt(rigth));
                    }
                    rigth++;
                    if(set.isEmpty()) {
                        break;
                    }
                }
                if(set.isEmpty()) {
                    ans = s.substring(i,rigth);
                }
                set.clear();
                for(int j = 0;j < tLength;j++) {
                    set.add(t.charAt(j));
                }
            }
            if(num == "") {
                num = ans;
            }
            if(ans.length() <= num.length()) {
                num = ans;
            }
            
        }
        return num;
    }
}